package com.zhang.es;

import com.google.gson.Gson;
import com.zhang.model.User;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.net.InetAddress;

import static org.elasticsearch.index.query.QueryBuilders.termQuery;

/**
 * @program: elasitcsearch
 * @description:
 * @author: Dawson.Zhang
 * @create: 2020-03-08
 **/
public class EsPutIndex {
    private TransportClient client;

    /**
     * 初始化client
     *
     * @throws Exception
     */
    @Before
    public void init() throws Exception {
        //1:创建一个setting，主要用于配置，设置连接集群地址
        Settings settings = Settings.builder()
            .put("cluster.name", "my-elasticsearch")
            .build();
        //创建一个Pbtc对象
        client = new PreBuiltTransportClient(settings)
            .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9301))
            .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9302))
            .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9303));
    }

    /**
     * 建立索引库
     *
     * @throws Exception
     */
    @Test
    public void CreateIndex() throws Exception {
        //1:创建一个Settings对象，相当于是一个配置信息。主要用户配置信息
        Settings settings = Settings.builder().put("cluster.name", "my-elasticsearch").build();
        PreBuiltTransportClient client = new PreBuiltTransportClient(settings);
        //2:创建一个Client对象

        client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9301));
        client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9302));
        client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9303));

        //3:使用Client创建一个索引库
        client.admin()
            .indices()
            .prepareCreate("java1")
            .get();
        //4:关闭Client
        client.close();
    }

    /**
     * 追加mappings
     *
     * @throws Exception
     */
    @Test
    public void AddMapping() throws Exception {
        //1:创建一个setting，主要用于配置，设置连接集群地址
        Settings settings = Settings.builder()
            .put("cluster.name", "my-elasticsearch")
            .build();
        //创建一个Pbtc对象
        PreBuiltTransportClient client = new PreBuiltTransportClient(settings);
        //添加节点
        client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9301))
            .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9302))
            .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9303));

//        {
//                "user": {
//                "properties": {
//                    "id": {
//                        "type": "long",
//                            "index": true
//                    },
//                    "username": {
//                        "type": "text",
//                            "index": true,
//                            "analyzer":"ik_smart"
//                    }
//                }
//            }
//        }
        //使用XContentBuilder追加mappings
        XContentBuilder builder = XContentFactory.jsonBuilder()
                  .startObject()
                        //设置类型
                       .startObject("user")
                        //设置properties
                            .startObject("properties")
                            //设置field
                                .startObject("id")
                                    .field("type", "long")
                                    .field("index", true)
                                .endObject()
                                .startObject("userName")
                                    .field("type", "text")
                                    .field("index", true)
                                    .field("store", true)
                                    .field("analyzer", "ik_max_word")
                                .endObject()
                            .endObject()
                        .endObject()
                    .endObject();

        client
            .admin()
            .indices()
            //使用Client把Mapping信息添加到索引库中
            .preparePutMapping("java1")
            //设置类型
            .setType("user")
            //mapping信息
            .setSource(builder)
            .get();
        client.close();
    }

    /**
     * 添加文档:方式一
     *
     * @throws Exception
     */
    @Test
    public void addDoc() throws IOException {
        XContentBuilder builder = XContentFactory.jsonBuilder()
            .startObject()
            .field("id", "3")
            .field("userName", "影流之主")
            .endObject();
        client.prepareIndex()
                .setIndex("java1")
                .setType("user")
                .setId("3")
                .setSource(builder)
                .get();
        client.close();
    }

    /**
     * 添加文档：方式二
     *
     * @throws Exception
     */
    @Test
    public void addDoc2() throws Exception {
        for (int i = 1; i < 100; i++) {
            User user = new User();
            user.setId(i);
            user.setUserName("荒漠领主"+i);

            Gson gson = new Gson();
            String s = gson.toJson(user);
            System.out.println(s);
            client.prepareIndex("java1", "user", ""+i+"")
                .setSource(s)
                .get();

        }
        client.close();
    }
    @Test
    public void delDoc(){
        client.prepareDelete("java1","user","14").get();
        client.close();
    }
    @Test
    public void updateDoc(){
        User user = new User();
        user.setId(10086);
        user.setUserName("荒漠领主");

        Gson gson = new Gson();
        String s = gson.toJson(user);
        System.out.println(s);
        client.prepareUpdate("java1","user","19").setDoc(s).get();
        client.close();
    }

}
